package dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import po.Teacher;

@Repository
public class TeacherDao {

	@Autowired
	private JdbcTemplate template;
	
	private TeacherRowMapper teacherRowMapper = new TeacherRowMapper();
	
	public void save(Teacher t){
		
		template.update("insert into teacher (t_id, t_name) values (?, ?)", t.getT_id(), t.getT_name());
	}
	
	public void update(Teacher t){
		
		template.update("update teacher set t_name=? where t_id=?", t.getT_name(), t.getT_id());
	}
	
	public void delete(int id){
		
		template.update("delete from teacher where t_id=?", id);
	}
	
	public Teacher get(int id){
		
		return template.queryForObject("select * from teacher where t_id=?", teacherRowMapper, id);
	}
	
	private static final class TeacherRowMapper implements RowMapper<Teacher>{

		@Override
		public Teacher mapRow(ResultSet paramResultSet, int paramInt) throws SQLException {
			
			Teacher t = new Teacher();
			t.setT_id(paramResultSet.getInt("t_id"));
			t.setT_name(paramResultSet.getString("t_name"));
			return t;
		}
	}
}
